//
//  QDMetroBillViewController.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/4/29.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 开发票的控制器
final class QDMetroBillViewController: QDWebViewController,WKUIDelegate {

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        hidesBottomBarWhenPushed = true
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        // 设置自定义的backItem
        let navigationBarItem = UIBarButtonItem(image: "bill_back".namedImage?.ritl_renderOrigin, style: .done, target: self, action: #selector(backDidTap))
        navigationItem.leftBarButtonItems = [navigationBarItem]
        
        
        // 设置给父类的隐藏属性
        setValue(navigationBarItem, forKeyPath: "backItem")
        
        autoTitle = false
        useLeftCloseItem = true
        leftCloseImage = "bill_close".namedImage
        
        // 关闭按钮
        leftCloseButtonTap = { [weak self] (_,_) in 
            self?.navigationController?.popViewController(animated: true)
        }
        
        // Do any additional setup after loading the view.
        webView.scrollView.ritl_setContentInsetAdjustmentBehavior {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        
        let scale = UIScreen.main.scale
        // bottom web的底部tabBar高度
        url = """
        \(invoiceUrl)?token=\(QDUserInfoModel.share.token)&bottom=\(BarDistance.TabBar.height / scale)&code=\(QDObtainUserIDFVTool.getIDHardwareCode())&lang=\(QDLanguage.localIdentifier.localForMetroBill)&debug=0&h=0#/invoice
        """
        print("\(QDObtainUserIDFVTool.getIDHardwareCode())")
        
        requestUrl()
    }
    
    
    /// 点击
    @objc func backDidTap() {
        if webView.canGoBack {
            webView.goBack(); return
        }
        navigationController?.popViewController(animated: true)
    }
    

    //
    override func autoRequestUrlAtViewDidLoad() -> Bool { return false }
    
    // MARK: Navigation
    // 导航阴影先隐藏
    override var nav_mutableBarShadowHidden: Bool { return true }
    // 手势关闭
    override var nav_backInteractive: Bool { return false }
    // 导航返回
    override var backIndicatorImageName: String? { return "bill_back" }
}



extension QDMetroBillViewController {

    override func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        //进行标题拦截decisionHandler
        guard let url = navigationAction.request.url, let scheme = url.scheme, scheme == "qdtitle"  else {
            super.webView(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler); return
        }
        
        //如果是qd_title,进行截取标题
        let title = (url.absoluteString as NSString).components(separatedBy: "//").last ?? ""
        //设置navigationItem
        navigationItem.title = (NSLocalizedString(title, comment: "发票国际化") as NSString).capitalized
        //设置shadow隐藏
        setNavigationBarShadowViewHidden(title.isEmpty)
        //
        decisionHandler(.cancel)
    }
}
